package com.decent.ejfadmin.finance.service.impl;

import com.decent.ejfadmin.common.bean.AlipayConfig;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupAlipay;
import com.decent.ejfadmin.common.enums.Consts;
import com.decent.ejfadmin.common.enums.SupRedisKey;
import com.decent.ejfadmin.common.util.Constants;
import com.decent.ejfadmin.common.util.DingTalkMessagePush;
import com.decent.ejfadmin.config.PlatUrlConfig;
import com.decent.ejfadmin.finance.dao.AlipayConfigDao;
import com.decent.ejfadmin.finance.service.AlipayConfigService;
import com.decent.ejfadmin.utils.SimpleCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 支付宝配置
 *
 * @author weicx
 * @date 2019/02/25
 */
@Service
public class AlipayConfigServiceImpl implements AlipayConfigService {
    @Autowired
    private AlipayConfigDao alipayConfigDao;
    @Autowired
    private DingTalkMessagePush dingTalkMessagePush;
    @Autowired
    private PlatUrlConfig platUrlConfig;
    /**
     * 查询支付宝配置
     *
     * @return
     */
    @Override
    public List<AlipayConfig> queryAlipayConfig() {
        List<AlipayConfig> alipayConfigList = alipayConfigDao.queryAlipayConfigs();
        for (AlipayConfig alipayConfig : alipayConfigList) {
            alipayConfig.setTodayPayMoney(alipayConfigDao.getAlipayWithdrawPriceSum(alipayConfig.getAlipay()));
        }
        return alipayConfigList;
    }

    /**
     * 修改限额
     *
     * @param alipayConfig 支付宝配置
     * @return
     */
    @Override
    public MessageBean updateAlipayConfig(AlipayConfig alipayConfig) {
        if (alipayConfigDao.updateAlipayConfig(alipayConfig) != 1) {
            return MessageBean.fail();
        }
        return MessageBean.success();
    }

    /**
     * 新增支付宝配置
     *
     * @param alipayConfig 支付宝配置
     * @return
     */
    @Override
    public MessageBean insertAlipayConfig(AlipayConfig alipayConfig) {
        if (alipayConfigDao.insertAlipayConfig(alipayConfig) != 1) {
            return MessageBean.fail();
        }
        return MessageBean.success();
    }

    /**
     * 切换支付宝
     *
     * @param alipayConfig 支付宝配置
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageBean updateAlipayState(AlipayConfig alipayConfig) {
        alipayConfigDao.updateAllStatus();
        if (alipayConfigDao.updateStatus(Consts.OPEN, alipayConfig.getId()) != 1) {
            return MessageBean.fail();
        }
        String contest = "支付宝提现切换提醒:\n支付宝提现切换至"+alipayConfigDao.getOpenAlipay(alipayConfig.getId())+"，请及时加款";
        dingTalkMessagePush.dingDingRobotPushing(contest, platUrlConfig.getAliPaySwitchHookToken());
        return MessageBean.success();
    }

    /**
     * 分页查询会员支付宝
     *
     * @param supAlipay 会员支付宝
     * @param page      分页组件
     * @return
     */
    @Override
    public Page querySupAlipayDetail(SupAlipay supAlipay, Page page) {
        int total = alipayConfigDao.supAlipayCount(supAlipay);
        if (total == 0) {
            page.setResultList(Collections.emptyList());
        } else {
            List<SupAlipay> supAlipays = alipayConfigDao.supAlipayDetail(supAlipay, page);
            page.setResultList(supAlipays);
        }
        page.setTotal(total);
        return page;
    }

    /**
     * 修改会员支付宝
     *
     * @param supAlipay 会员支付宝
     * @return
     */
    @Override
    public MessageBean updateSupAlipay(SupAlipay supAlipay) {
        if (alipayConfigDao.updateSupAlipay(supAlipay) != 1) {
            return MessageBean.fail();
        }
        return MessageBean.success();
    }

    /**
     * 删除会员支付宝
     *
     * @param supAlipay 会员支付宝
     * @return
     */
    @Override
    public MessageBean deleteSupAlipay(SupAlipay supAlipay) {
        if (alipayConfigDao.updateSupAlipayStatus(supAlipay.getId()) != 1) {
            return MessageBean.fail();
        }
        return MessageBean.success();
    }

    /**
     * 支付宝提现开关 true 开启 false 关闭
     *
     * @return
     */
    @Override
    public boolean getAlipayStatus() {
        Object autoAudit = SimpleCacheUtil.getObject(SupRedisKey.ALIPAY_WITHDRAW_FLAG.toString());
        if (Objects.isNull(autoAudit)) {
            return true;
        }
        return false;
    }

    /**
     * 支付宝提现开关 true 开启 false 关闭
     *
     * @param status
     * @return
     */
    @Override
    public MessageBean changeAlipayStatus(Integer status) {
        if (null == status) {
            return MessageBean.fail("参数有误");
        }
        if (Constants.STATE_OPEN.intValue() == status) {
            SimpleCacheUtil.removeObject(SupRedisKey.ALIPAY_WITHDRAW_FLAG.toString());
        } else {
            SimpleCacheUtil.setObject(SupRedisKey.ALIPAY_WITHDRAW_FLAG.toString(), SupRedisKey.ALIPAY_WITHDRAW_FLAG.toString());
        }
        return MessageBean.success();
    }

}
